/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.modelDemand.service;

import com.fzhucc.modules.modelDemand.domain.ModelDemandAuditLog;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.modelDemand.repository.ModelDemandAuditLogRepository;
import com.fzhucc.modules.modelDemand.domain.dto.ModelDemandAuditLogDto;
import com.fzhucc.modules.modelDemand.domain.dto.ModelDemandAuditLogQueryCriteria;
import com.fzhucc.modules.modelDemand.service.mapstruct.ModelDemandAuditLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-01-09
**/
@Service
@RequiredArgsConstructor
public class ModelDemandAuditLogService {

    private final ModelDemandAuditLogRepository modelDemandAuditLogRepository;
    private final ModelDemandAuditLogMapper modelDemandAuditLogMapper;

    public Map<String,Object> queryAll(ModelDemandAuditLogQueryCriteria criteria, Pageable pageable){
        Page<ModelDemandAuditLog> page = modelDemandAuditLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(modelDemandAuditLogMapper::toDto));
    }

    public List<ModelDemandAuditLogDto> queryAll(ModelDemandAuditLogQueryCriteria criteria){
        return modelDemandAuditLogMapper.toDto(modelDemandAuditLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public ModelDemandAuditLogDto findById(Integer id) {
        ModelDemandAuditLog modelDemandAuditLog = modelDemandAuditLogRepository.findById(id).orElseGet(ModelDemandAuditLog::new);
        ValidationUtil.isNull(modelDemandAuditLog.getId(),"ModelDemandAuditLog","id",id);
        return modelDemandAuditLogMapper.toDto(modelDemandAuditLog);
    }

    @Transactional(rollbackFor = Exception.class)
    public ModelDemandAuditLogDto create(ModelDemandAuditLog resources) {
        return modelDemandAuditLogMapper.toDto(modelDemandAuditLogRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ModelDemandAuditLog resources) {
        ModelDemandAuditLog modelDemandAuditLog = modelDemandAuditLogRepository.findById(resources.getId()).orElseGet(ModelDemandAuditLog::new);
        ValidationUtil.isNull( modelDemandAuditLog.getId(),"ModelDemandAuditLog","id",resources.getId());
        modelDemandAuditLog.copy(resources);
        modelDemandAuditLogRepository.save(modelDemandAuditLog);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            modelDemandAuditLogRepository.deleteById(id);
        }
    }

    public void download(List<ModelDemandAuditLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ModelDemandAuditLogDto modelDemandAuditLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("建议", modelDemandAuditLog.getAdvice());
            map.put("劳模工匠    企业需求      分类", modelDemandAuditLog.getType());
            map.put("阶段      区审核通过   区审核驳回   市审核通过  市审核驳回", modelDemandAuditLog.getStage());
            map.put("被审核的 劳模或需求id", modelDemandAuditLog.getSportdid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}